package com.wjw.leetcode;

import java.util.Arrays;

/**
 * merge-sorted-array
 * 合并两个有序数组
 */
public class leetcode88 {

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] arr = new int[m + n];
        int i = 0, j = 0, k = 0;
        while (i < m && j < n) {
            if (nums1[i] < nums2[j]) {
                arr[k++] = nums1[i++];
            } else {
                arr[k++] = nums2[j++];
            }
        }

        if (i < m) {
            System.arraycopy(nums1, i, arr, k, m - i);
        }
        if (j < n) {
            System.arraycopy(nums2, j, arr, k, n - j);
        }
        System.arraycopy(arr, 0, nums1, 0, m + n);
    }

    public void merge1(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i < n; i++) {
            nums1[m + i] = nums2[i];
        }
        Arrays.sort(nums1);
    }
}
